package com.wen.controller;

import com.github.pagehelper.PageInfo;
import com.wen.pojo.Permission;
import com.wen.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") Integer size) throws Exception {
        ModelAndView model = new ModelAndView();
        List<Permission> all = permissionService.findAll(page, size);
        PageInfo pageInfo = new PageInfo(all);
        model.addObject("pageInfo", pageInfo);
        model.setViewName("permission-list");
        return model;
    }

    @RequestMapping("/findById")
    public ModelAndView findById(Integer id) throws Exception {
        ModelAndView model = new ModelAndView();
        Permission byId = permissionService.findById(id);
        model.addObject("permission", byId);
        model.setViewName("permission-show");
        return model;

    }

    @RequestMapping("/deleteById")
    public String deleteById(Integer id) throws Exception {
        permissionService.deleteById(id);
        return "redirect:/permission/findAll";
    }

    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate( Integer id) throws Exception {
        ModelAndView model=new ModelAndView();
        Permission byId = permissionService.findById(id);
        model.addObject("permission", byId);
        model.setViewName("permission-update");
        return model;

    }

    @RequestMapping("/updateById")
    public String updateById(Permission permission) throws Exception {
        permissionService.updateById(permission);
        return "redirect:/permission/findAll";
    }

    @RequestMapping("/add")
    public String add(Permission permission) throws Exception {
        permissionService.add(permission);
        return "redirect:/permission/findAll";
    }
}
